/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//
import wifi from '@ohos.wifi_native_js';
import prompt from '@system.prompt'
import '../../common/StringExtend.js'
export default {
    data: {
        wifiInfo:{
            enable:false,
            isActive:false,
            scanInfo:[]
        },
        wifiPassword:'',
        wifichecked:{},
        connectItem:{},
        isScaning:false,
    },
    computed:{
        state(){
            let str="当前未连接";
            if(!this.wifiInfo.isActive){
                str="请先打开WIFI"
            }else{
                if(this.connectItem && this.connectItem.ssid){
                    str=`已连接:${this.connectItem.ssid.utf8ToStr()}`
                }else{
                    str="当前未连接"
                }
            }
            return str;
        },
        WifiScanList(){
            let list=this.wifiInfo.scanInfo;
            if(!JSON.stringify(this.connectItem)=='{}'){
                list=list.splice(list.indexOf(this.connectItem),1);
            }
            return list;
        },
        checkedWifiName(){
            if(this.wifichecked && this.wifichecked.ssid){
                return this.wifichecked.ssid.utf8ToStr();
            }
            return "";
        }
    },
    onInit() {
      this.wifiInfo.isActive=wifi.isWifiActive();
      this.wifiScaning();
    },
    //WiFi开启关闭按钮点击事件
    wifiSwitchChanged({checked}){
        wifi.getLinkedInfo(t=>{
            prompt.showToast({
                message:JSON.stringify(t),
                duration:2500
            })
        })
        this.wifiInfo.isActive=checked;
        if(checked){
            let wifistate=false;
            wifistate=wifi.enableWifi();
            if(!wifistate){
               prompt.showToast({
                   message:"wifi开启失败请重试",
                   duration:1500
               })
               this.wifiInfo.isActive=false;
               return;
            }
            this.wifiScaning();
        }else{
            this.wifiInfo.scanInfo=[];
            this.wifichecked={}
            wifi.disableWifi();
        }
    },
    //下拉刷新WiFi列表
    refreshTouched({state}){
        if(state=="start" && this.wifiInfo.isActive)
            this.wifiScaning();
    },
    //扫描wifi
    wifiScaning(){
        this.isScaning=true;
        wifi.scan();
        wifi.getScanInfos((result)=>{
            let ss=JSON.parse(JSON.stringify(result));
            prompt.showToast({
                message:"获取到wifi扫描列表,当前共有"+ss.length+"个wifi",
                duration:1500
            })
            this.wifiInfo.scanInfo=ss;
            this.isScaning=false;
        });
    },
    //选中wifi弹出输入密码框
    checkedWifi(data){
        this.wifichecked=data.detail;
        this.showInputPassword();
    },
    //连接按钮事件
    connect(){
        this.hideInputPassword()
        this.connectWifi();
    },
    //连接wifi
    connectWifi(){
        let WifiDeviceConfig={
            ssid:this.wifichecked.ssid,
            bssid:"",
            preSharedKey:this.wifiPassword,
            isHiddenSsid:false,
            securityType:this.wifichecked.securityType
        }
        let status= wifi.connectToDevice(WifiDeviceConfig);
        if(status){
            this.connectItem=this.wifichecked
        }else{
            prompt.showToast({
                message:"连接失败，请检查密码是否正确",
                duration:1500
            })
        }
        wifi.getLinkedInfo(t=>{
            prompt.showToast({
                message:JSON.stringify(t),
                duration:2500
            })
        })
    },
    //显示键盘
    showKeyBoard(){
        this.$element("keyboard_dialog").show();
    },
    //关闭键盘显示
    hideKeyBoard(){
         this.$element("keyboard_dialog").close();
    },
    //显示输入WiFi密码
    showInputPassword(){
        this.$element("wifi_dialog").show();
    },
    //关闭输入WiFi密码
    hideInputPassword(){
        this.$element("keyboard_dialog").close();
        this.$element("wifi_dialog").close();
        this.wifiPassword='';
    },
    //键盘回调事件
    keyboardChange(value){
        this.wifiPassword= value.detail
    }


}



